<?php
/**
 * CK 开发框架
 * User: Clake
 * Date: 15/10/15
 * Time: 14:52
 */

namespace CK\Util;


use CK\Core\Component;
use CK\Util\IO\Directory;

/**
 * Class Excel
 * EXCEL 导出功能类
 * @package CK\Util
 */
class Excel extends Component{
    /**
     * 传入娄组导出为EXCEL格式
     * @param string $excel_name    导出EXCEL名称
     * @param array  $data          要导出的数据
     * @param array  $head          表格头信息
     * @param string $excel_type    导出EXCEL类型,默认为 EXCEL2007
     *
     * @return string
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     */
    public function exportData($excel_name,$data,$head=null,$excel_type='Excel2007') {
        $excel = new \PHPExcel();
        $excel->getProperties()->setCreator("Warehouse System");
        $excel->getProperties()->setLastModifiedBy("Warehouse System");
        $excel->getProperties()->setTitle("Query Export To Excel");
        $excel->getProperties()->setSubject("Export To Excel");
        $excel->getProperties()->setDescription("");
        $excel->getProperties()->setKeywords("");
        $excel->getProperties()->setCategory("Excel File");
        $excel->setActiveSheetIndex(0);

        $sheet = $excel->getActiveSheet();
        $excel_head = [];
        $excel_head_key = [];

        //build excel column head and column key
        if ($head && is_array($head)) {
            foreach ($head as $column) {
                $excel_head[] =  $column['text'];
                $excel_head_key[] = $column['name'];
            }
        } else {
            $result = $data[0];
            $excel_head = array_keys($result);
            $excel_head_key = $excel_head;
        }

        //头样式
        $head_style = [
            'alignment' => [
                'horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER
            ],
            'font' => [
                'bold' => true,
                'size' => 14
            ]
        ];

        //添加Excel头信息
        $column = ord("A");
        $A = "";
        $index = 1;
        foreach ($excel_head as $key) {
            if($column >= 91) {
                $column = ord("A");
                $A = "A";
            }
// 			$sheet->getColumnDimension($A.chr($column))->setWidth(intval($value / 6));
            $sheet->getColumnDimension($A.chr($column))->setAutoSize(true);
 			$sheet->getStyle($A.chr($column)."$index")->applyFromArray($head_style);
            $sheet->setCellValue($A.chr($column)."$index",$key);
            $column++;
        }
        $index++;

        //添加Excel内容
        foreach ($data as $result) {
            $column = ord("A");
            $A = "";
            foreach ($excel_head_key as $key) {
                if($column >= 91) {
                    $column = ord("A");
                    $A = "A";
                }
                $sheet->setCellValue($A.chr($column).$index,$result[$key]?$result[$key]:"");
                $column++;
            }
            $index++;
        }

        //检查否存在临时输出目录
        $export_dir = APP_CACHE_PATH.'excel/';

        if (!Directory::Exists($export_dir)) {
            Directory::Create($export_dir);
        }

        $ext = $excel_type == 'Excel2007' ? '.xlsx':'.xls';

        $ext_name = date('_Ymd').$ext;

        $writer = \PHPExcel_IOFactory::createWriter($excel,$excel_type);

        $writer->save($export_dir.$excel_name.$ext_name);
        return $excel_name.$ext_name;
    }
}